﻿using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BW.Mes.WinForms.BasicData;

namespace BW.Mes.WinForms.BasicData
{
    public partial class BomParent : DevExpress.XtraEditors.XtraForm
    {
        private long _parentId;
        public BomParent(long parentId = 0)
        {
            InitializeComponent();
            _parentId = parentId;
            this.Text = "添加子配件";
            InitForm();
        }

        // 定义输入控件
        private TextBox txtProductCode, txtProductName, txtSpecification, txtUnit, txtSpend, txtNumber, txtBomBfb;

        private void BomParent_Load(object sender, EventArgs e)
        {

        }

        private Button btnSave;

        private void InitForm()
        {
            this.Width = 400;
            this.Height = 400;
            var lbl1 = new Label { Text = "物品编号:", Left = 30, Top = 30, Width = 80 };
            txtProductCode = new TextBox { Left = 120, Top = 30, Width = 200 };
            var lbl2 = new Label { Text = "物品名称:", Left = 30, Top = 70, Width = 80 };
            txtProductName = new TextBox { Left = 120, Top = 70, Width = 200 };
            var lbl3 = new Label { Text = "规格型号:", Left = 30, Top = 110, Width = 80 };
            txtSpecification = new TextBox { Left = 120, Top = 110, Width = 200 };
            var lbl4 = new Label { Text = "单位:", Left = 30, Top = 150, Width = 80 };
            txtUnit = new TextBox { Left = 120, Top = 150, Width = 200 };
            var lbl5 = new Label { Text = "品牌:", Left = 30, Top = 190, Width = 80 };
            txtSpend = new TextBox { Left = 120, Top = 190, Width = 200 };
            var lbl6 = new Label { Text = "配件数量:", Left = 30, Top = 230, Width = 80 };
            txtNumber = new TextBox { Left = 120, Top = 230, Width = 200 };
            var lbl7 = new Label { Text = "损耗率:", Left = 30, Top = 270, Width = 80 };
            txtBomBfb = new TextBox { Left = 120, Top = 270, Width = 200 };
            btnSave = new Button { Text = "保存", Left = 120, Top = 320, Width = 80 };
            btnSave.Click += BtnSave_Click;
            this.Controls.AddRange(new Control[] { lbl1, txtProductCode, lbl2, txtProductName, lbl3, txtSpecification, lbl4, txtUnit, lbl5, txtSpend, lbl6, txtNumber, lbl7, txtBomBfb, btnSave });
        }

        private void BtnSave_Click(object sender, EventArgs e)
        {
            // 组装配件对象
            var sparepart = new BomSparepartViewModel
            {
                ProductCode = txtProductCode.Text.Trim(),
                ProductName = txtProductName.Text.Trim(),
                Specification = txtSpecification.Text.Trim(),
                Unit = txtUnit.Text.Trim(),
                Spend = txtSpend.Text.Trim(),
                NNumber = int.TryParse(txtNumber.Text.Trim(), out int n) ? n : 0,
                BomBfb = txtBomBfb.Text.Trim(),
                ParentId = _parentId
            };
            // TODO: 保存到数据库或API
            MessageBox.Show($"已组装子配件对象，父ID={_parentId}\n物品编号={sparepart.ProductCode}", "提示");
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
    }
}